﻿using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TreeNode subNode = new TreeNode("C"); //目录名称
            subNode.Tag = "C:\\";
            treeView.Nodes.Add(subNode);
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = treeView.SelectedNode;
            if (node == null) 
            {
                return;
            }
            
            string path = node.Tag.ToString();
            DirectoryInfo dir = new DirectoryInfo(path);
            DirectoryInfo[] dirs;
            FileInfo[] files;
            try
            {
                dirs= dir.GetDirectories();
                files = dir.GetFiles();
            }
            catch(Exception ex)
            {
                return;
            }
            listView.Items.Clear();
            foreach (FileInfo f in files)
            {
                ListViewItem lv = new ListViewItem(f.Name);
                lv.SubItems.AddRange(new string[] { f.Extension, (f.Length / 1024).ToString() + "KB" });
                lv.Tag = f.FullName;
                listView.Items.Add(lv);
            }

            if (node.Nodes.Count > 0) return;
            foreach (DirectoryInfo d in dirs)
            {
                TreeNode subNode = new TreeNode(d.Name);
                subNode.Tag = d.FullName;
                node.Nodes.Add(subNode);
            }   
        }

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            ListViewItem listViewItem = listView.SelectedItems[0];
            if (listViewItem == null) return;
            string path = listViewItem.Tag.ToString();
            FileInfo file = new FileInfo(path);
            string ex = file.Extension;
            if (".exe".Equals(ex))
            {
                System.Diagnostics.Process.Start(path);
            }
            else if (".txt".Equals(ex))
            {
                System.Diagnostics.Process.Start("notepad.exe", path);
            }
        }

        private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem listViewItem = listView.SelectedItems[0];
            if (listViewItem == null) return;
            string path = listViewItem.Tag.ToString();
            FileInfo file = new FileInfo(path);
            string ex = file.Extension;
            if (".exe".Equals(ex))
            {
                System.Diagnostics.Process.Start(path);
            }
            else if (".txt".Equals(ex))
            {
                System.Diagnostics.Process.Start("notepad.exe", path);
            }
        }

        private void 删除文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem listViewItem = listView.SelectedItems[0];
            if (listViewItem == null) return;
            string path = listViewItem.Tag.ToString();
            if (MessageBox.Show("是否删除？", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                File.Delete(path);
                int index = listView.SelectedItems[0].Index; 
                listView.Items.RemoveAt(index);
            }
        }
    }
}
